/**
 * 党员信息管理系统 - 测试数据生成脚本
 * 用于生成符合系统要求的党员信息测试数据
 */

// 工具函数：生成随机日期
function generateRandomDate(startYear = 2000, endYear = 2024) {
  const start = new Date(startYear, 0, 1);
  const end = new Date(endYear, 11, 31);
  const randomTime = start.getTime() + Math.random() * (end.getTime() - start.getTime());
  return new Date(randomTime).toISOString().split('T')[0];
}

// 工具函数：生成随机手机号
function generatePhoneNumber() {
  const prefixes = ['130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '150', '151', '152', '153', '155', '156', '157', '158', '159', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189'];
  const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];
  const suffix = Math.floor(Math.random() * 100000000).toString().padStart(8, '0');
  return prefix + suffix;
}

// 工具函数：生成随机身份证号
function generateIdCard() {
  const areas = ['110101', '310101', '440101', '500101', '120101'];
  const area = areas[Math.floor(Math.random() * areas.length)];
  const birth = generateRandomDate(1995, 2005).replace(/-/g, '');
  const sequence = Math.floor(Math.random() * 999).toString().padStart(3, '0');
  const checkCode = Math.floor(Math.random() * 10);
  return area + birth + sequence + checkCode;
}

// 工具函数：生成随机QQ号
function generateQQNumber() {
  return Math.floor(Math.random() * 900000000) + 100000000;
}

// 样本数据
const sampleNames = ['张三', '李四', '王五', '赵六', '陈七', '刘八', '杨九', '黄十'];
const sampleNationalities = ['汉族', '满族', '蒙古族', '回族', '藏族', '维吾尔族', '苗族', '彝族', '壮族', '布依族', '朝鲜族', '侗族'];
const sampleMajors = ['计算机科学与技术', '软件工程', '信息管理与信息系统', '电子信息工程', '通信工程'];
const sampleClasses = ['计算机2101班', '计算机2102班', '软件2101班', '软件2102班', '信息2101班'];
const sampleBirthPlaces = ['北京市', '上海市', '广东省深圳市', '江苏省南京市', '浙江省杭州市'];
const sampleVolunteerActivities = ['社区志愿服务', '环保宣传活动', '敬老院慰问', '支教活动', '疫情防控志愿服务'];

// --- 修改点：新增和更新了样本数据 ---
const sampleAwards = ['三好学生', '优秀学生干部', '学习优秀奖', '科技创新奖', '社会实践先进个人', '国家奖学金'];
const sampleIssuingAuthorities = ['东北大学', '共青团中央', '辽宁省教育厅', '软件学院'];
const sampleOrganizations = ['东北大学软件学院', '东北大学计算机学院', '东北大学理学院'];
const samplePartyBranches = ['2021级本科生第一党支部', '2021级本科生第二党支部', '2022级研究生党支部'];
const samplePositions = ['无', '支部委员', '组织委员', '宣传委员'];


/**
 * 生成单个党员信息数据
 * @param {number} index - 序号，用于生成唯一标识
 * @returns {Object} 完整的党员信息对象
 */
function generateMemberData(index = 1) {
  const birthYear = 1998 + Math.floor(Math.random() * 5);
  const birthDate = generateRandomDate(birthYear, birthYear);
  const enrollmentDate = generateRandomDate(2020, 2022);
  // 默认设置为未毕业，只有少数设置为已毕业
  const isGraduated = Math.random() < 0.1; // 10%的概率为已毕业
  const graduationDate = isGraduated ? generateRandomDate(2024, 2024) : null;

  // --- 修改点：为党支部历史生成结构化数据 ---
  const currentBranchName = samplePartyBranches[Math.floor(Math.random() * samplePartyBranches.length)];
  const joinDate = generateRandomDate(2021, 2022);
  const leaveDate = isGraduated ? generateRandomDate(2024, 2024) : null;
  
  const partyBranchHistory = [{
    startDate: joinDate,
    endDate: leaveDate,
    organizationName: sampleOrganizations[0],
    branchName: currentBranchName,
    positionHeld: samplePositions[Math.floor(Math.random() * samplePositions.length)]
  }];
  
  // 随机选择政治面貌，确保有足够的正式党员和预备党员
  const politicalStatuses = ['正式党员', '预备党员'];
  const politicalStatus = politicalStatuses[Math.floor(Math.random() * politicalStatuses.length)];
  
  // 根据是否毕业设置学籍状态
  const enrollmentStatus = isGraduated ? '已毕业' : '在读';

  return {
    // 基本信息
    id: `member-${index}`,
    memberId: `member-${index}`,
    memberName: sampleNames[index % sampleNames.length] + index,
    idCardNumber: generateIdCard(),
    birthPlace: sampleBirthPlaces[Math.floor(Math.random() * sampleBirthPlaces.length)],
    residenceLocation: sampleBirthPlaces[Math.floor(Math.random() * sampleBirthPlaces.length)],
    gender: Math.random() > 0.5 ? '男' : '女', // 修改字段名以匹配组件
    genderType: Math.random() > 0.5 ? '男' : '女',
    nationalityDesc: sampleNationalities[Math.floor(Math.random() * sampleNationalities.length)],
    birthDate: birthDate,
    enrollmentDate: enrollmentDate,
    graduationDate: graduationDate,
    phoneNumber: generatePhoneNumber(),
    qqNumber: generateQQNumber().toString(),
    class: sampleClasses[Math.floor(Math.random() * sampleClasses.length)], // 修改字段名以匹配组件
    studentClass: sampleClasses[Math.floor(Math.random() * sampleClasses.length)],
    studentId: (2020000000 + index).toString(),
    majorField: sampleMajors[Math.floor(Math.random() * sampleMajors.length)],
    politicalStatus: politicalStatus,
    enrollmentStatus: enrollmentStatus, // 添加学籍状态字段
    memberRemarks: `备注信息${index}`,
    joinLeagueDate: generateRandomDate(2013, 2018),
    
    // 添加joinDate和leaveDate到根级别，便于组件使用
    joinDate: joinDate,
    leaveDate: leaveDate,

    // --- 修改点：新增 currentPartyBranch 字段，并更新 partyBranchHistory 结构 ---
    currentPartyBranch: currentBranchName,
    partyBranchHistory: partyBranchHistory,

    volunteerActivities: [{
      activityName: sampleVolunteerActivities[Math.floor(Math.random() * sampleVolunteerActivities.length)],
      activityDate: generateRandomDate(2020, 2023),
      performanceDesc: '表现优秀，获得好评'
    }, {
      activityName: sampleVolunteerActivities[Math.floor(Math.random() * sampleVolunteerActivities.length)],
      activityDate: generateRandomDate(2021, 2024),
      performanceDesc: '积极参与，认真负责'
    }],

    // --- 修改点：字段重命名为 awardsReceived 并更新数据结构 ---
    awardsReceived: [{
      awardDate: generateRandomDate(2021, 2022),
      awardName: sampleAwards[Math.floor(Math.random() * sampleAwards.length)],
      issuingAuthority: sampleIssuingAuthorities[Math.floor(Math.random() * sampleIssuingAuthorities.length)]
    }, {
      awardDate: generateRandomDate(2022, 2023),
      awardName: sampleAwards[Math.floor(Math.random() * sampleAwards.length)],
      issuingAuthority: sampleIssuingAuthorities[Math.floor(Math.random() * sampleIssuingAuthorities.length)]
    }],


    // 入党申请书
    applicationForm: {
      signatureDate: generateRandomDate(2021, 2022)
    },

    // 谈心谈话记录
    heartToHeartTalk: {
      talkerName: '导师姓名' + index,
      signatureDate: generateRandomDate(2022, 2023)
    },

    // 积极分子推荐表
    activistRecommendation: {
      formFillDate: generateRandomDate(2022, 2023),
      branchRecommendDate: generateRandomDate(2022, 2023),
      baseCommitteeApprovalDate: generateRandomDate(2022, 2023),
      schoolCommitteeReviewDate: generateRandomDate(2022, 2023),
      developmentTargetDate: generateRandomDate(2023, 2024)
    },

    // 积极分子考察表
    activistAssessment: {
      activistDesignationDate: generateRandomDate(2022, 2023),
      mentorList: [{
        mentorName: '培养人1',
        joinPartyDate: generateRandomDate(2015, 2020),
        workTogetherStart: generateRandomDate(2020, 2022),
        mentorStartDate: generateRandomDate(2022, 2023)
      }, {
        mentorName: '培养人2',
        joinPartyDate: generateRandomDate(2016, 2020),
        workTogetherStart: '1.5年',
        mentorStartDate: generateRandomDate(2022, 2023)
      }],
      secretarySignature: '党支部书记签名',
      secretarySignatureDate:'2022-12-11',
      mainPerformanceDesc: '该同志表现优秀，思想进步...',
      assessmentRecords: [{
        mentor1Name: '培养人1',
        mentor2Name: '培养人2',
        mentorOpinionDate: generateRandomDate(2023, 2024),
        branchSecretaryName: '党支部书记',
        branchOpinionDate: generateRandomDate(2023, 2024)
      }],
      trainingStartDate: generateRandomDate(2023, 2024),
      trainingEndDate: generateRandomDate(2023, 2024),
      developmentTrainingStartDate: generateRandomDate(2024, 2024),
      developmentTrainingEndDate: generateRandomDate(2024, 2024),
      committeeOpinionDate: generateRandomDate(2024, 2024),
      committeeSignature: '党支部书记签名',
      massDiscussionOrganizerSignature: '组织委员签名',
      massDiscussionDate: generateRandomDate(2024, 2024)
    },

    // 积极分子时期思想汇报
    activistThoughtReports: [{
      signatureDate: generateRandomDate(2023, 2024)
    }, {
      signatureDate: generateRandomDate(2023, 2024)
    }, {
      signatureDate: generateRandomDate(2023, 2024)
    }],

    // 群众意见
    massOpinion: {
      branchSecretarySignature: '党支部书记签名',
      signatureDate: generateRandomDate(2024, 2024)
    },

    // 自传
    autobiography: {
      signatureDate: generateRandomDate(2024, 2024)
    },

    // 政审外调
    externalInvestigations: [{
      personName: '父亲姓名',
      relationshipDesc: '父子关系',
      signatureDate: generateRandomDate(2024, 2024)
    }, {
      personName: '母亲姓名',
      relationshipDesc: '母子关系',
      signatureDate: generateRandomDate(2024, 2024)
    }],

    // 政审综合
    comprehensiveReview: {
      committeeSignature:'测试',
      signatureDate: generateRandomDate(2024, 2024)
    },

    // 发展对象预审
    developmentTargetPrereview: {
      signatureDate: generateRandomDate(2024, 2024)
    },

    // 发展党员公示结果
    developmentPublicity: {
      startDate: generateRandomDate(2024, 2024),
      endDate: generateRandomDate(2024, 2024),
      signatureDate: generateRandomDate(2024, 2024)
    },

    // 入党志愿书
    applicationVolunteer: {
      personalSignatureDate: generateRandomDate(2024, 2024),
      sponsor1Signature: '入党介绍人1签名',
      sponsor1SignatureDate: generateRandomDate(2024, 2024),
      sponsor2Signature: '入党介绍人2签名',
      sponsor2SignatureDate: generateRandomDate(2024, 2024),
      branchMeetingSecretaryName: '党支部书记姓名',
      branchMeetingSignatureDate: generateRandomDate(2024, 2024),
      designatedTalkDate: generateRandomDate(2024, 2024),
      generalBranchReviewDate: generateRandomDate(2024, 2024),
      baseCommitteeApprovalOpinionDate: generateRandomDate(2024, 2024),
      probationaryConversionResolutionSignature: '党支部书记签名',
      probationaryConversionResolutionDate: generateRandomDate(2024, 2024),
      generalBranchReviewProbationDate: generateRandomDate(2024, 2024),
      baseCommitteeReviewProbationDate: generateRandomDate(2024, 2024)
    },

    // 接收预备党员表决情况汇总表
    probationaryAcceptanceVoting: {
      signatureDate: generateRandomDate(2024, 2024)
    },

    // 预备党员考察表
    probationaryAssessment: {
      assessmentOpinions: [{
        signatureDate:'2022-01-02',
        sponsor1Name: '介绍人1姓名',
        sponsor2Name: '介绍人2姓名',
        sponsorOpinionDate: generateRandomDate(2024, 2024),
        branchSecretaryName: '党支部书记姓名',
        branchOpinionDate: generateRandomDate(2024, 2024)
      }]
    },

    // 预备期思想汇报
    probationaryThoughtReports: [{
      signatureDate: generateRandomDate(2024, 2024)
    }, {
      signatureDate: generateRandomDate(2024, 2024)
    }],

    // 转正申请书
    conversionApplication: {
      signatureDate: generateRandomDate(2024, 2024)
    },

    // 预备党员转正公示结果
    conversionPublicity: {
      startDate: generateRandomDate(2024, 2024),
      endDate: generateRandomDate(2024, 2024),
      signatureDate: generateRandomDate(2024, 2024)
    },

    // 预备党员转正表决汇总表
    conversionVotingSummary: {
      signatureDate: generateRandomDate(2024, 2024)
    }
  };
}

/**
 * 批量生成党员信息数据
 * @param {number} count - 生成数量
 * @returns {Array} 党员信息数组
 */
export function generateMemberDataBatch(count = 10) {
  const members = [];
  for (let i = 1; i <= count; i++) {
    members.push(generateMemberData(i));
  }
  return members;
}

// 生成一些特定的测试数据，便于测试功能
export function generateTestMembers() {
  return [
    // 添加一些特定的测试党员数据
    {
      memberId: 'test-member-1',
      memberName: '张测试',
      studentId: '2021000001',
      gender: '男',
      politicalStatus: '正式党员',
      enrollmentStatus: '在读',
      class: '计算机2101班',
      phoneNumber: '13800138001',
      joinDate: '2021-09-01',
      leaveDate: null
    },
    {
      memberId: 'test-member-2',
      memberName: '李预党',
      studentId: '2021000002',
      gender: '女',
      politicalStatus: '预备党员',
      enrollmentStatus: '在读',
      class: '软件2101班',
      phoneNumber: '13800138002',
      joinDate: '2022-03-15',
      leaveDate: null
    },
    {
      memberId: 'test-member-3',
      memberName: '王毕业',
      studentId: '2020000003',
      gender: '男',
      politicalStatus: '正式党员',
      enrollmentStatus: '已毕业',
      class: '信息2101班',
      phoneNumber: '13800138003',
      joinDate: '2020-09-01',
      leaveDate: '2024-06-30'
    }
  ];
}
